//
//  WorkFlowBottomView.m
//  YunKang
//
//  Created by leoking870 on 2017/11/10.
//  Copyright © 2017 leoking870. All rights reserved.
//

#import "WorkFlowBottomView.h"
#import "YKProcessTrackingViewController.h"
#import "YKAuditSuggestViewController.h"
#import "YKPopupView.h"
#import "WorkFlowManager.h"

@interface WorkFlowBottomView ()
@property(nonatomic, strong) UIButton *repealOrderButton;
@property(nonatomic, strong) UIButton *processTrackButton;
@property(nonatomic, strong) UIButton *repositionOrderButton;
@property(nonatomic, strong) UIButton *requestRevokeButton;
@property(nonatomic, strong) UIButton *agreeRefundButton;
@end


@implementation WorkFlowBottomView

- (instancetype)initWithType:(WorkFlowBottomViewType)type {
    if (self = [super init]) {

        switch (type) {
            case WorkFlowBottomViewTypeNormal: {
                UIButton *repealOrderButton = [[UIButton alloc] init];
                [self addSubview:repealOrderButton];
                UIButton *processTrackButton = [[UIButton alloc] init];
                [self addSubview:processTrackButton];
                UIButton *repositionOrderButton = [[UIButton alloc] init];
                [self addSubview:repositionOrderButton];
                self.repealOrderButton = repealOrderButton;
                self.processTrackButton = processTrackButton;
                self.repositionOrderButton = repositionOrderButton;
                [repealOrderButton setTitle:@"订单驳回" forState:UIControlStateNormal];
                [repositionOrderButton setTitle:@"提交审批" forState:UIControlStateNormal];
                [processTrackButton setTitle:@"流程跟踪" forState:UIControlStateNormal];


                [repositionOrderButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];//
                [repositionOrderButton setBackgroundImage:TFSTYLE(buttonDisableBackgroundImage) forState:UIControlStateDisabled];//

                [processTrackButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];//
                [processTrackButton setBackgroundImage:TFSTYLE(buttonDisableBackgroundImage) forState:UIControlStateDisabled];//
                [repealOrderButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];//
                [repealOrderButton setBackgroundImage:TFSTYLE(buttonDisableBackgroundImage) forState:UIControlStateDisabled];//

                [repealOrderButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
                [repositionOrderButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
                [processTrackButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];

                repealOrderButton.titleLabel.font = [UIFont systemFontOfSize:14];
                repositionOrderButton.titleLabel.font = [UIFont systemFontOfSize:14];
                processTrackButton.titleLabel.font = [UIFont systemFontOfSize:14];

                [processTrackButton addTarget:self action:@selector(processTrack:) forControlEvents:UIControlEventTouchUpInside];
                [repealOrderButton addTarget:self action:@selector(repealOrder:) forControlEvents:UIControlEventTouchUpInside];
                [repositionOrderButton addTarget:self action:@selector(repositionOrder:) forControlEvents:UIControlEventTouchUpInside];
            }
                break;
            case WorkFlowBottomViewTypeRefunding: {
                self.requestRevokeButton = [[UIButton alloc] init];
                [self addSubview:self.requestRevokeButton];

                self.agreeRefundButton = [[UIButton alloc] init];
                [self addSubview:self.agreeRefundButton];

                [self.requestRevokeButton setTitle:@"申请驳回" forState:UIControlStateNormal];
                [self.agreeRefundButton setTitle:@"同意退货" forState:UIControlStateNormal];

                [self.requestRevokeButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor tf_colorWithHexString:@"#f1f1f1"]] forState:UIControlStateNormal];//
                [self.requestRevokeButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor grayColor]] forState:UIControlStateDisabled];//
                [self.agreeRefundButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor tf_colorWithHexString:@"f1f1f1"]] forState:UIControlStateNormal];//
                [self.agreeRefundButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor grayColor]] forState:UIControlStateDisabled];//
                [self.agreeRefundButton setTitleColor:TFSTYLE(buttonEnableBackgroundColor) forState:UIControlStateNormal];
                [self.requestRevokeButton setTitleColor:TFSTYLE(buttonEnableBackgroundColor) forState:UIControlStateNormal];
                self.requestRevokeButton.titleLabel.font = [UIFont systemFontOfSize:14];
                self.agreeRefundButton.titleLabel.font = [UIFont systemFontOfSize:14];
                [self.requestRevokeButton addTarget:self action:@selector(requestRevoke:) forControlEvents:UIControlEventTouchUpInside];
                [self.agreeRefundButton addTarget:self action:@selector(agreeRefund:) forControlEvents:UIControlEventTouchUpInside];
            }

                break;
            case WorkFlowBottomViewTypeRefunded: {
                self.requestRevokeButton = [[UIButton alloc] init];
                [self addSubview:self.requestRevokeButton];
                self.processTrackButton = [[UIButton alloc] init];
                [self addSubview:self.processTrackButton];
                self.agreeRefundButton = [[UIButton alloc] init];
                [self addSubview:self.agreeRefundButton];
                [self.processTrackButton setTitle:@"流程跟踪" forState:UIControlStateNormal];
                [self.requestRevokeButton setTitle:@"申请驳回" forState:UIControlStateNormal];
                [self.agreeRefundButton setTitle:@"同意退货" forState:UIControlStateNormal];
                [self.processTrackButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor tf_colorWithHexString:@"f1f1f1"]] forState:UIControlStateNormal];//
                [self.processTrackButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor grayColor]] forState:UIControlStateDisabled];//
                [self.processTrackButton setTitleColor:TFSTYLE(buttonEnableBackgroundColor) forState:UIControlStateNormal];
                self.processTrackButton.titleLabel.font = [UIFont systemFontOfSize:14];

                [self.processTrackButton addTarget:self action:@selector(processTrack:) forControlEvents:UIControlEventTouchUpInside];

                [self.requestRevokeButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor tf_colorWithHexString:@"#f1f1f1"]] forState:UIControlStateNormal];//
                [self.requestRevokeButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor grayColor]] forState:UIControlStateDisabled];//
                [self.agreeRefundButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor tf_colorWithHexString:@"f1f1f1"]] forState:UIControlStateNormal];//
                [self.agreeRefundButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor grayColor]] forState:UIControlStateDisabled];//

                self.requestRevokeButton.titleLabel.font = [UIFont systemFontOfSize:14];
                self.agreeRefundButton.titleLabel.font = [UIFont systemFontOfSize:14];
                [self.requestRevokeButton addTarget:self action:@selector(requestRevoke:) forControlEvents:UIControlEventTouchUpInside];
                [self.agreeRefundButton addTarget:self action:@selector(agreeRefund:) forControlEvents:UIControlEventTouchUpInside];

                self.requestRevokeButton.enabled = NO;
                self.agreeRefundButton.enabled = NO;
            }
                break;
        }

        [self.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
        [self.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.height.mas_equalTo(44);
        }];
    }
    return self;
}

- (void)requestRevoke:(id)sender {

    YKAuditSuggestViewController *vc = [YKAuditSuggestViewController new];
    vc.submitBlock = ^(NSString *text) {
        [TFHTTPMANAER POST:@"customerOrder/updateReturnOrderById.htm" parameters:@{@"orderId": self.ID, @"roId": self.ROID, @"remark": text ?: @"", @"status": @"3"} HUD:@"驳回中..." success:^(id responseObject) {
            [[UIApplication sharedApplication].keyWindow.rootViewController showHint:@"驳回成功"];
            self.requestRevokeButton.enabled = NO;
            self.agreeRefundButton.enabled = NO;
        }          failure:^(id responseObject, NSError *error) {
            [[UIApplication sharedApplication].keyWindow.rootViewController showHint:error.localizedFailureReason];
        }];
    };
    [YKPopupView showPopupViewWithViewController:vc];

}

- (void)agreeRefund:(id)sender {
    [TFHTTPMANAER POST:@"customerOrder/updateReturnOrderById.htm" parameters:@{@"orderId": self.ID, @"roId": self.ROID, @"remark": @"", @"status": @"2"} HUD:@"同意中..." success:^(id responseObject) {
        [[UIApplication sharedApplication].keyWindow.rootViewController showHint:@"同意退货成功"];
        self.requestRevokeButton.enabled = NO;
        self.agreeRefundButton.enabled = NO;
    }          failure:^(id responseObject, NSError *error) {
        [[UIApplication sharedApplication].keyWindow.rootViewController showHint:error.localizedFailureReason];
    }];
}

- (void)setRO_BUSINESS_EXAMINESTATUS:(NSString *)RO_BUSINESS_EXAMINESTATUS {
    _RO_BUSINESS_EXAMINESTATUS = RO_BUSINESS_EXAMINESTATUS;
    self.requestRevokeButton.enabled = [_RO_BUSINESS_EXAMINESTATUS isEqualToString:@"1"];
    self.agreeRefundButton.enabled = [_RO_BUSINESS_EXAMINESTATUS isEqualToString:@"1"];
}


- (void)setBUSINESSEXAMINESTATUS:(NSString *)BUSINESSEXAMINESTATUS {
    _BUSINESSEXAMINESTATUS = BUSINESSEXAMINESTATUS;
    self.repealOrderButton.enabled = [_BUSINESSEXAMINESTATUS isEqualToString:@"1"];
    if ([_BUSINESSEXAMINESTATUS isEqualToString:@"2"] || [_BUSINESSEXAMINESTATUS isEqualToString:@"3"]) {
        self.repealOrderButton.enabled = NO;
        self.repositionOrderButton.enabled = NO;
    }
}

- (void)processTrack:(id)sender {
    YKProcessTrackingViewController *vc = [YKProcessTrackingViewController new];
    vc.m_id = self.ID;
    [YKPopupView showPopupViewWithViewController:vc];
}

/**
 订单驳回
 */
- (void)repealOrder:(id)sender {
    YKAuditSuggestViewController *vc = [YKAuditSuggestViewController new];
    vc.submitBlock = ^(NSString *text) {
        [TFHTTPMANAER POST:@"customerOrder/updateOrderInfoById.htm" parameters:@{@"orderId": self.ID, @"examinestatus": @"3", @"orderreturnreason": text} HUD:@"订单驳回中..." success:^(id responseObject) {
            [[UIApplication sharedApplication].keyWindow.rootViewController showHint:@"订单驳回成功"];
            self.repealOrderButton.enabled = NO;
            self.repositionOrderButton.enabled = NO;
        }          failure:^(id responseObject, NSError *error) {
            [[UIApplication sharedApplication].keyWindow.rootViewController showHint:error.localizedDescription];
        }];
    };
    [YKPopupView showPopupViewWithViewController:vc];
}

/**
 订单转审
 */
- (void)repositionOrder:(id)sender {
    [WorkFlowManager repositionApproveWithBusinessID:self.ID fromViewController:[UIApplication sharedApplication].keyWindow.rootViewController completion:^(BOOL succcess) {
        if (succcess) {
            [[UIApplication sharedApplication].keyWindow.rootViewController showHint:@"提交审批成功"];
            self.repealOrderButton.enabled = NO;
            self.repositionOrderButton.enabled = NO;
        }
    }];
}

@end
